﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;
using System.Collections;

public class Dogtag
{
    public GUIModule Module;
    private DogtagItem ownerTag;
    public static Texture BackIcon;
    public static Texture HpIcon;
    public static Texture MpIcon;
    public Dogtag(GUIModule module)
    {
        Module = module;
        BackIcon = Resources.Load("GUI/icon_MC_01") as Texture2D;
        HpIcon = Resources.Load("GUI/icon_MC_HP") as Texture2D;
        MpIcon = Resources.Load("GUI/icon_MC_MP") as Texture2D;

        RegionStateManager.LoginStateChangeHandle += delegate(LoginState form, LoginState to)
        {
            if (to == LoginState.LoginEnd)
            {
                DogtagItem tmpTag = new DogtagItem(AppInterface.AvatarModule.IOwner);
                getSanpshot(tmpTag.Handle, delegate(Texture2D t)
                {
                    ownerTag = tmpTag;
                    ownerTag.HeadIcon = t;
                });
                AppInterface.AvatarModule.IOwner.OnHpChange += new Action<IUnit, int>(AvatarHpChange);
                AppInterface.AvatarModule.IOwner.OnMpChange += new Action<IAvatarHandle, int>(AvatarMpChange);
            }
            if (to == LoginState.LogoutEnd)
            {
                if (AppInterface.AvatarModule.IOwner != null)
                {
                    AppInterface.AvatarModule.IOwner.OnHpChange -= new Action<IUnit, int>(AvatarHpChange);
                    AppInterface.AvatarModule.IOwner.OnMpChange -= new Action<IAvatarHandle, int>(AvatarMpChange);
                }
                GameObject.DestroyImmediate(ownerTag.HeadIcon);
                ownerTag = null;
            }
        };
        Module.OnScreenChange += delegate(int width, int height)
        {
            for (int i = 0; i < defaultRects.Length; i++)
            {
                rects[i].xMin = defaultRects[i].xMin * Module.WidthScale;
                rects[i].yMin = defaultRects[i].yMin * Module.HeightScale;
                rects[i].width = defaultRects[i].width * Module.WidthScale;
                rects[i].height = defaultRects[i].height * Module.HeightScale;
            }
            if (ownerTag != null)
            {
                ownerTag.HP = ownerTag.Handle.Hp / (ownerTag.Handle.AllHp * 1.0f);
                rects[2].width = defaultRects[2].width * Module.WidthScale * ownerTag.HP;
            }
        };
    }
    private void AvatarHpChange(IUnit av, int tHp)
    {
        if (ownerTag != null)
        {
            ownerTag.HP = av.Hp / (av.AllHp * 1.0f);
            rects[2].width = defaultRects[2].width * Module.WidthScale * ownerTag.HP;
        }
    }
    private void AvatarMpChange(IAvatarHandle av, int tMp)
    {

    }
    private Rect[] defaultRects = new Rect[]
    {
        new Rect(5, 5, 193, 84),//new Rect(5, 5, 191, 78),
        new Rect(22, 20, 55, 55),//new Rect(35, 10, 64, 64),
        new Rect(89, 46, 92, 12),//new Rect(103, 32, 87, 10),
        new Rect(89, 65, 92, 12),//new Rect(103, 45, 87, 10),
        new Rect(95, 25, 100, 25)
    };
    public Rect[] rects = new Rect[]
    {
        new Rect(0, 0, 0, 0),
        new Rect(0, 0, 0, 0),
        new Rect(0, 0, 0, 0),
        new Rect(0, 0, 0, 0),
        new Rect(0, 0, 0, 0)
    };
    public void OnGUI()
    {
        if (ownerTag != null)
        {
            GUI.DrawTexture(rects[1], ownerTag.HeadIcon);
            GUI.DrawTexture(rects[0], BackIcon);
            GUI.DrawTexture(rects[2], HpIcon);
            GUI.DrawTexture(rects[3], MpIcon);
            GUI.Label(rects[4], "LV.7 小叮当");
        }
    }
    private GameObject snapshotGameObject;
    private void getSanpshot(IAvatarHandle handle, Action<Texture2D> callBack)
    {
        Define.Mono.StartCoroutine(_getSanpshot(handle, callBack));
    }
    private IEnumerator _getSanpshot(IAvatarHandle handle, Action<Texture2D> callBack)
    {
        if (snapshotGameObject == null)
        {
            snapshotGameObject = GameObject.Instantiate(Resources.Load("Other/HeadIcon_pre")) as GameObject;
        }

        snapshotGameObject.SetActiveRecursively(true);
        snapshotGameObject.transform.position = handle.HeadTransform.position;
        snapshotGameObject.transform.rotation = handle.HeadTransform.rotation;
        Texture2D rTexture = null;
        Camera camera = snapshotGameObject.GetComponentInChildren<Camera>();
        camera.Render();

        yield return new WaitForEndOfFrame();

        RenderTexture.active = camera.targetTexture;
        camera.pixelRect = new Rect(0, 0, 128, 128);
        rTexture = new Texture2D((int)camera.pixelRect.width, (int)camera.pixelRect.height, TextureFormat.ARGB32, false);
        rTexture.ReadPixels(camera.pixelRect, 0, 0);
        rTexture.Apply();
        camera.enabled = false;
        snapshotGameObject.SetActiveRecursively(false);
        callBack(rTexture);
    }
}
public class DogtagItem
{
    public IAvatarHandle Handle;
    public float HP = 0;
    public DogtagItem(IAvatarHandle handle)
    {
        Handle = handle;
        HP = 1;
    }
    public Texture2D HeadIcon;
}

